package com.youlu.campus.admin.order;

import com.youlu.campus.admin.auth.entity.SystemRole;
import com.youlu.campus.admin.auth.service.RoleService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.entity.VO.req.DigitalCaOrderReq;
import com.youlu.campus.service.order.DigitalCaOrderDetailService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class DigitalCaOrderController {

    @Autowired
    private DigitalCaOrderDetailService digitalCaOrderDetailService;
    @Autowired
    private RoleService roleService;

    @PostMapping("/digital-ca-order/list")
    public QResult orderPage(@RequestBody DigitalCaOrderReq digitalCaOrderReq) {
        String userId = AuthUtils.getUserId();
        digitalCaOrderReq.setUserId(userId);
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        long count = roleList.stream().filter(role -> role.getMobileDesensitization()).count();
        if(count>0){
            digitalCaOrderReq.setMobileDesensitization(Boolean.TRUE);
            log.info("【手机号脱敏】当前用户 ->{} 手机号脱敏",AuthUtils.getLoginName());
        }
        long countMobileHide = roleList.stream().filter(role -> role.getMobileHide()).count();
        if(countMobileHide>0){
            log.info("【手机号隐藏】当前用户 ->{} 手机号隐藏",AuthUtils.getLoginName());
            digitalCaOrderReq.setMobileHide(Boolean.TRUE);
        }
        return QResult.success(digitalCaOrderDetailService.list(digitalCaOrderReq));
    }

    @PostMapping("/digital-ca-order/export")
    public QResult orderExport(@RequestBody DigitalCaOrderReq digitalCaOrderReq) {
        String userId = AuthUtils.getUserId();
        digitalCaOrderReq.setUserId(userId);
        digitalCaOrderReq.setOptUserName(AuthUtils.getLoginName());
        List<SystemRole> roleList = roleService.findAllByUserId(userId);
        long count = roleList.stream().filter(role -> role.getMobileDesensitization()).count();
        if(count>0){
            digitalCaOrderReq.setMobileDesensitization(Boolean.TRUE);
            log.info("【手机号脱敏】当前用户 ->{} 手机号脱敏",AuthUtils.getLoginName());
        }
        long countMobileHide = roleList.stream().filter(role -> role.getMobileHide()).count();
        if(countMobileHide>0){
            log.info("【手机号隐藏】当前用户 ->{} 手机号隐藏",AuthUtils.getLoginName());
            digitalCaOrderReq.setMobileHide(Boolean.TRUE);
        }
        digitalCaOrderDetailService.exportCaOrder(digitalCaOrderReq);
        return QResult.success(null);
    }

}
